﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HOOMT
{
    public partial class ObjectModelForm : OriginalModelForm
    {
        #region 属性
        //public  HighOrderObjectDiagram highOrderObjectDiagram = null;
        #endregion
        //####################################################################################
        #region 构造函数
        public ObjectModelForm(MainForm m)
        {
            parentMainForm = m;//获取父窗口的引用
            /*if (supForm == null)//设置Form在treeView的路径
            {
                preTreeNode = parentMainForm.treeView.Nodes[0].Nodes[0];
            }
            else
            {
                preTreeNode = supForm.preTreeNode;
            }*/
           
            
        }
        #endregion

        #region 窗口属性改变函数集
        //************************************************************************************
        protected override void ModelForm_Click(object sender, EventArgs e)
        {
            this.BringToFront();
            this.Focus();
            //parentMainForm.currentDiagramFlag = Diagram.NullObject;//图形对象标志复位为空?????
            parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.stateTransitionsModelToolStrip);
            parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.informationFlowModelToolStrip);
            parentMainForm.splitContainer3.Panel1.Controls.Add(parentMainForm.objectModelToolStrip);
        }
        //************************************************************************************
        protected override void ModelForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.objectModelToolStrip);
            e.Cancel = true;
            base.OnClosing(e);
            this.Hide();
        }
        //************************************************************************************
        protected override void ModelForm_Resize(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Minimized)
            {
                this.BringToFront();
                this.Focus();
                parentMainForm.currentDiagramFlag = Diagram.NullObject;//图形对象标志复位为空
                parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.stateTransitionsModelToolStrip);
                parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.informationFlowModelToolStrip);
                parentMainForm.splitContainer3.Panel1.Controls.Add(parentMainForm.objectModelToolStrip);
            }
            else
            {
                parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.objectModelToolStrip);
            }
        }
        //************************************************************************************
        //重写WinProc函数拦截消息，处理窗体标题栏点击事件
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0xa1 && (int)m.WParam == 0x2)
            {
                parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.stateTransitionsModelToolStrip);
                parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.informationFlowModelToolStrip);
                parentMainForm.splitContainer3.Panel1.Controls.Add(parentMainForm.objectModelToolStrip);
            }
            base.WndProc(ref m);
        }
        #endregion

        #region 图形对象鼠标响应函数集
        //************************************************************************************
        protected override void ModelForm_MouseEnter(object sender, EventArgs e)
        {
            //if (this.Focused)
            {
                switch (parentMainForm.currentDiagramFlag)
                {
                    case Diagram.HighOrderObject:
                        Cursor = System.Windows.Forms.Cursors.Cross;
                        break;
                    case Diagram.PrimitiveObject:
                        Cursor = System.Windows.Forms.Cursors.Cross;
                        break;
                    case Diagram.SimpleRelationship:
                        Cursor = System.Windows.Forms.Cursors.UpArrow;
                        break;
                    case Diagram.ComplexRelationship:
                        Cursor = System.Windows.Forms.Cursors.UpArrow;
                        break;
                    case Diagram.NullObject:
                        Cursor = System.Windows.Forms.Cursors.Default;
                        break;
                }
            }
        }
        //************************************************************************************
        protected override void ModelForm_MouseDown(object sender, MouseEventArgs e)
        {
            #region 左键双击
            if (e.Clicks == 2 && e.Button == MouseButtons.Left)
            {
                #region 双击弹出对话框
                //双击弹出对话框或进入下层设计
                foreach (OriginalDiagram diag in diagramList)
                {
                    //当图形对象为HighOrderObjectDiagram类的对象
                    if (diag is HighOrderObjectDiagram)
                    {
                        HighOrderObjectDiagram obj = (HighOrderObjectDiagram)diag;
                        Rectangle rect = new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y, 2 * diag.Width / 3, diag.Height);
                        Rectangle rect1 = new Rectangle(diag.UpLeftpoint.X + 2 * diag.Width / 3, diag.UpLeftpoint.Y + diag.Height / 7, diag.Width / 3, diag.Height / 7);
                        Rectangle rect2 = new Rectangle(diag.UpLeftpoint.X + 2 * diag.Width / 3, diag.UpLeftpoint.Y + 3 * diag.Height / 7, diag.Width / 3, diag.Height / 7);
                        Rectangle rect3 = new Rectangle(diag.UpLeftpoint.X + 2 * diag.Width / 3, diag.UpLeftpoint.Y + 5 * diag.Height / 7, diag.Width / 3, diag.Height / 7);
                        Point pt = new Point(e.X, e.Y);
                        //处理显示平台
                        if (rect.Contains(pt))
                        {
                            //图形对象当前没有下层分解平台，构造新MainForm，以无模式对话框形式弹出
                            if (!obj.HasModelForm)
                            {
                                ObjectModelForm modelForm = new ObjectModelForm(parentMainForm);//
                                modelForm.Text = "ObjectModelForm/ " + obj.Name;//图形对象名字与其包含的MainForm名字一致//

                                parentMainForm.modelFormList.Add(modelForm);//添加Form对象到MainForm的对象列表modelFormList
                                //parentMainForm.diagramList_List.Add(modelForm.diagramList);//添加Form对象的对象链表到MainForm的对象列表diagramList_List
                                modelForm.TopLevel = false;
                                parentMainForm.splitContainer3.Panel2.Controls.Add(modelForm);
                                //modelForm.preTreeNode = obj.treeNode;
                                //增加相应树节点
                                //TreeNode node = new TreeNode("Main");
                                //node.ContextMenuStrip = parentMainForm.formMenuStrip;//增加响应菜单（右击）
                                //node.Tag = modelForm;//节点指向绘制载体
                                //obj.treeNode.Nodes.Add(node);
                                //modelForm.preTreeNode = this.preTreeNode.Nodes[obj.highDiagramNum + 1];//更新Form的preTreeNode
                                //modelForm.preTreeNode.Nodes.Add("Main");//增加Form
                                //modelForm.formNodeFullPath = obj.diagramNodeFullPath + "\\Main";//fullPath,用于删除对象
                                //modelForm.preTreeNode.Nodes[1].ContextMenuStrip = parentMainForm.formMenuStrip;//增加响应菜单（右击）
                                
                                obj.NewModelForm = modelForm;
                                obj.HasModelForm = true;

                                //新建的mainForm属于obj对象(无需从面板导航到对象)
                                //modelForm.highOrderObjectDiagram = obj;
;
                                modelForm.BringToFront();
                                modelForm.Show();
                                modelForm.Focus();
                            }
                            //图形对象当前有下层分解平台，原有的非模式窗口获得焦点
                            else
                            {
                                obj.NewModelForm.BringToFront();
                                obj.NewModelForm.Show();
                                obj.NewModelForm.Focus();
                            }
                            break;
                        }
                        //处理方法
                        else if (rect1.Contains(pt))
                        {
                            //方法对话框与图形对象方法列表成员之间的数据交换
                            MethodsDialog dlg = new MethodsDialog();
                            StringBuilder stringBuilder = new StringBuilder();
                            foreach (Method method in obj.MethodList)
                            {
                                stringBuilder.AppendLine(method.ToString());
                                //dlg.TemporaryMethodList.Clear();
                                dlg.TemporaryMethodList.Add(method);//临时列表同步对象方法列表
                            }
                            //更新方法对话框显示
                            dlg.MethodShowBox.Text = stringBuilder.ToString();
                            if (dlg.ShowDialog() == DialogResult.OK)
                            {
                                //更新图形对象数据
                                obj.MethodList.Clear();//清空原有方法链表
                                foreach (Method method in dlg.TemporaryMethodList)
                                {
                                    obj.MethodList.Add(method);//更新方法链表
                                }
                            }
                            Invalidate();
                            break;
                        }
                        //处理属性
                        else if (rect2.Contains(pt))
                        {
                            AttributesDialog dlg = new AttributesDialog();
                            StringBuilder stringBuilder = new StringBuilder();
                            foreach (Attribute attr in obj.AttributeList)
                            {
                                stringBuilder.AppendLine(attr.ToString());
                                dlg.TemporaryAttributeList.Add(attr);//临时列表同步对象属性列表
                            }
                            //更新方法对话框显示
                            dlg.AttributeShowBox.Text = stringBuilder.ToString();
                            if (dlg.ShowDialog() == DialogResult.OK)
                            {
                                //更新图形对象数据
                                obj.AttributeList.Clear();//清空原有方法链表
                                foreach (Attribute attr in dlg.TemporaryAttributeList)
                                {
                                    obj.AttributeList.Add(attr);
                                }
                            }
                            Invalidate();
                            break;
                        }
                        //处理约束￥￥￥￥￥
                        else if (rect3.Contains(pt))
                        {
                            NonFunctionDialog dlg = new NonFunctionDialog();
                            StringBuilder stringBuilder = new StringBuilder();
                            foreach (NonFunctionRequirment requirment in obj.NonFunctionRequirmentList)
                            {
                                stringBuilder.AppendLine(requirment.ToString());
                                dlg.TemporaryNFRList.Add(requirment);//临时列表同步对象属性列表
                            }
                            //更新方法对话框显示
                            dlg.NonFunctionShowBox.Text = stringBuilder.ToString();
                            if (dlg.ShowDialog() == DialogResult.OK)
                            {
                                //更新图形对象数据
                                obj.NonFunctionRequirmentList.Clear();//清空原有方法链表
                                foreach (NonFunctionRequirment requirment in dlg.TemporaryNFRList)
                                {
                                    obj.NonFunctionRequirmentList.Add(requirment);
                                }
                            }
                            Invalidate();
                            break;
                        }
                    }
                    //当图形对象为PrimitiveObjectDiagram类的对象
                    else if (diag is PrimitiveObjectDiagram)
                    {
                        PrimitiveObjectDiagram obj = (PrimitiveObjectDiagram)diag;
                        Rectangle rect1 = new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y, diag.Width, diag.Height / 3);
                        Rectangle rect2 = new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y + diag.Height / 3, diag.Width, diag.Height / 3);
                        Rectangle rect3 = new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y + 2 * diag.Height / 3, diag.Width, diag.Height / 3);
                        Point pt = new Point(e.X, e.Y);
                        if (rect1.Contains(pt))
                        {
                            //方法对话框与图形对象方法列表成员之间的数据交换
                            MethodsDialog dlg = new MethodsDialog();
                            StringBuilder stringBuilder = new StringBuilder();
                            foreach (Method method in obj.MethodList)
                            {
                                stringBuilder.AppendLine(method.ToString());
                                dlg.TemporaryMethodList.Add(method);
                            }
                            //更新方法对话框显示
                            dlg.MethodShowBox.Text = stringBuilder.ToString();
                            if (dlg.ShowDialog() == DialogResult.OK)
                            {
                                //更新图形对象数据
                                obj.MethodList.Clear();
                                foreach (Method method in dlg.TemporaryMethodList)
                                {
                                    obj.MethodList.Add(method);
                                }
                                //对象的赋值为指向同一个对象
                                //obj.MethodList = dlg.TemporaryMethodList;
                            }
                            Invalidate();
                            break;
                        }
                        //处理属性￥￥￥￥￥
                        else if (rect2.Contains(pt))
                        {
                            AttributesDialog dlg = new AttributesDialog();
                            StringBuilder stringBuilder = new StringBuilder();
                            foreach (Attribute attr in obj.AttributeList)
                            {
                                stringBuilder.AppendLine(attr.ToString());
                                dlg.TemporaryAttributeList.Add(attr);
                            }
                            //更新方法对话框显示
                            dlg.AttributeShowBox.Text = stringBuilder.ToString();
                            if (dlg.ShowDialog() == DialogResult.OK)
                            {
                                //更新图形对象数据
                                obj.AttributeList.Clear();
                                foreach (Attribute attr in dlg.TemporaryAttributeList)
                                {
                                    obj.AttributeList.Add(attr);
                                }
                            }
                            Invalidate();
                            break;
                        }
                        //处理约束￥￥￥￥￥
                        else if (rect3.Contains(pt))
                        {
                            NonFunctionDialog dlg = new NonFunctionDialog();
                            StringBuilder stringBuilder = new StringBuilder();
                            foreach (NonFunctionRequirment requirment in obj.NonFunctionRequirmentList)
                            {
                                stringBuilder.AppendLine(requirment.ToString());
                                dlg.TemporaryNFRList.Add(requirment);//临时列表同步对象属性列表
                            }
                            //更新方法对话框显示
                            dlg.NonFunctionShowBox.Text = stringBuilder.ToString();
                            if (dlg.ShowDialog() == DialogResult.OK)
                            {
                                //更新图形对象数据
                                obj.NonFunctionRequirmentList.Clear();//清空原有方法链表
                                foreach (NonFunctionRequirment requirment in dlg.TemporaryNFRList)
                                {
                                    obj.NonFunctionRequirmentList.Add(requirment);
                                }
                            }
                            Invalidate();
                            break;
                        }
                    }
                }
                #endregion
            }
            #endregion

            #region 鼠标左击
            else if(/*e.Button == MouseButtons.Left*/true)
            {
                ClearSelection();
                Point pt = new Point(e.X, e.Y);
                //添加图形对象
                if ((parentMainForm.currentDiagramFlag == Diagram.HighOrderObject || parentMainForm.currentDiagramFlag == Diagram.PrimitiveObject))
                {
                    AddDiagram(parentMainForm.currentDiagramFlag, pt);
                }
                //画线准备
                else if ((parentMainForm.currentDiagramFlag == Diagram.SimpleRelationship || parentMainForm.currentDiagramFlag == Diagram.ComplexRelationship))
                {
                    DrawingLinePre(pt);
                }
                //选中操作
                else if (parentMainForm.currentDiagramFlag == Diagram.NullObject)
                {
                    SelectDiagram(pt);
                    SelecteLine(pt);
                }
                //缩放准备
                if (preSelectedDiagram != null)
                {
                    selectedMagnifyingDiagramPoint(pt);
                }
            }
            #endregion

            #region 鼠标右击
            if (e.Button == MouseButtons.Right)
            {
                ShowContextMenuStrip(new Point(e.X, e.Y));
            }
            #endregion

            #region 文字同步
            //使所有对象的NameBox失去焦点
            parentMainForm.objectModelToolStrip.Focus();
            RenameProcess();
            #endregion
        }
        //************************************************************************************
        protected override void ModelForm_MouseMove(object sender, MouseEventArgs e)
        {
            //移动
            Point pt = new Point(e.X, e.Y);
            if (selectedDiagram != null && movingDiagramFlag)
            {
                MovingDiagram(selectedDiagram, pt);
            }
            //缩放
            else if (preSelectedDiagram != null && magnifyingDiagramFlag)
            {
                magnifyingDiagram(pt);
            }
            //画线
            else if (lineStartDiagram != null && drawingLineFlag)
            {
                DrawingLine(pt);
            }
        }
        //************************************************************************************
        protected override void ModelForm_MouseUp(object sender, MouseEventArgs e)
        {
            Point pt = new Point(e.X, e.Y);
            //移动
            if (selectedDiagram != null && movingDiagramFlag)
            {
                MovedDiagram(selectedDiagram, pt);
            }
            //缩放
            else if (preSelectedDiagram != null && magnifyingDiagramFlag)
            {
                magnifiedDiagram(pt);
            }
            //画线
            else if (lineStartDiagram != null && drawingLineFlag)
            {
                DrawedLine(parentMainForm.currentDiagramFlag, pt);
            }
            base.ModelForm_MouseUp(sender, e);
        }
        #endregion
    }
}
  
 